

export default class BoundarySelector {
    constructor(n) {
        this.n = n;
        this.indices = new Int8Array(n);
        this.tmpIndices = new Int8Array(n);
        let _g = 0;
        while(_g < n) {
            let i = _g++;
            this.indices[i] = i;
        }
    }
    getIndex(i) {
        return this.indices[i];
    }
    select(index) {
        let i = 0;
        while(this.indices[i] !== index) ++i;
        while(i > 0) {
            let tmp = this.indices[i];
            this.indices[i] = this.indices[i - 1];
            this.indices[i - 1] = tmp;
            --i;
        }
    }
    setSize(size) {
        let indices=this.indices,tmpIndices=this.tmpIndices;
        let numSmaller = 0, numGreater = 0;
        let _g = 0;
        let _g1 = this.n;
        while(_g < _g1) {
            let idx = indices[_g++];
            if(idx < size) {
                tmpIndices[numSmaller] = idx;
                ++numSmaller;
            } else {
                tmpIndices[size + numGreater] = idx;
                ++numGreater;
            }
        }
        this.indices = tmpIndices;
        this.tmpIndices = indices;
    }
}